package com.shambles.manage.filter;

import com.shambles.manage.mapper.UsersMapper;
import com.shambles.manage.model.Users;
import com.shambles.manage.tools.MyTestUtil;
import com.shambles.manage2.tkmapper.entity.Usersinfo;
import com.shambles.manage2.tkmapper.mapper.UsersinfoMapper;
import com.shambles.sqlserver.context.Connect;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;

public class MobileAssetIsLoginFilter implements Filter {

    ApplicationContext applicationContext = new Connect().get();

    private UsersMapper usersMapper;

    private UsersinfoMapper usersinfoMapper;

    public FilterConfig configAsset = null;

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        System.out.println("dofilter");
        HttpServletRequest hrequest = (HttpServletRequest) request;
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) response);

        String loginStrings = configAsset.getInitParameter("loginStrings");
        String includeStrings = configAsset.getInitParameter("includeStrings");
        String redirectPath = hrequest.getContextPath() + configAsset.getInitParameter("redirectPath");
        String mobileLoginPath = hrequest.getContextPath() + configAsset.getInitParameter("mobileLoginPath");
        String settingPath = hrequest.getContextPath() + configAsset.getInitParameter("settingPath");
        String disabletestfilter = configAsset.getInitParameter("disabletestfilter");


        String[] loginList = loginStrings.split(";");
        String[] includeList = includeStrings.split(";");

        if (!IsLoginFilter.isContains(hrequest.getRequestURI(), includeList)) {
            chain.doFilter(request, response);
            return;
        }

        if (IsLoginFilter.isContains(hrequest.getRequestURI(), loginList)) {//
            chain.doFilter(request, response);
            return;
        }

        String openId = (String) hrequest.getSession().getAttribute("openId");

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        WebApplicationContext wac = WebApplicationContextUtils    //controller浠ュ鐨勫寘鍒濆鍖栫被
                .getRequiredWebApplicationContext(httpRequest.getSession()
                        .getServletContext());

        usersMapper = wac.getBean(UsersMapper.class);
        usersinfoMapper = wac.getBean(UsersinfoMapper.class);
        if (openId == null) {
            MyTestUtil.print(hrequest.getSession());
            System.out.println("MobileAssetIsLoginFilter openid= null");
            wrapper.sendRedirect(mobileLoginPath);
            return;
        } else {
            Users users = usersMapper.getUserInfoByOpenId(openId);
            System.out.println("MobileAssetIsLoginFilter openId =" + openId);

            String name = users.getName();
            String phone = users.getPhone();

            if (phone == null || "".equals(phone) || "".equals(name) || name == null) {
                wrapper.sendRedirect(settingPath);
                return;
            } else {
                //根据openID查询userinfo表
                Usersinfo usersinfo = usersinfoMapper.findByOpenID(openId);
                try {
                    if (phone.equals(usersinfo.getPhone()) && name.equals(usersinfo.getName())) {
                        chain.doFilter(request, response);
                    } else {
                        wrapper.sendRedirect(redirectPath);
                        return;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                    wrapper.sendRedirect(redirectPath);
                    return;
                }
                return;
            }
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        configAsset = filterConfig;
    }
}
